<template>
    <view class="goods">
        <view class="cu-card article no-card">
            <view class="cu-item goods-item solid-bottom" style="border-radius: 16rpx;" v-for="(item, index) in goodsList" :key="index">
                <!-- <view class="content" @click="click1(item.goodsSpu.id,shopInfo.id)"> -->
                <view class="content">
                    <image v-if="item.goodsSpu" :src="
            item.goodsSpu.picUrls[0]
              ? item.goodsSpu.picUrls[0]
              : '/static/public/img/no-exchange.png'
          " mode="aspectFill" class="row-img"></image>
                    <image v-else src="/static/public/img/no-exchange.png" mode="aspectFill" class="row-img"></image>
                    <view class="desc block padding-top-xs">
                        <view class="overflow-1 margin-top-sm">
                            <text class="text-black margin-top-sm">{{item.name}} {{item.specs?item.specs:''}}</text>
                        </view>
                        <view class="text-gray text-sm overflow-1">月销：{{ item.saleNum }}</view>
                        <view class="text-gray text-sm overflow-1">限购：{{ item.limitQuantity }}件</view>
                        <!-- <view class="text-gray text-sm overflow-1" v-if="item.goodsSpu.factory">生产厂家：{{ item.goodsSpu.factory }}
                        </view> -->
                        <view class="text-gray text-sm">所需积分：<text class="text-df text-red text-bold">
                                {{ item.goodsPoints }}</text></view>
                        <view class="flex justify-between">
                            <view></view>
                            <view class="round buy text-sm shadow bg-red" :class="'bg-' + theme.themeColor"
                                v-if="item.type == 1" @click.stop="
                  payHandel(index, item.goodsPoints, item.quantity)
                ">
                                <text> 立即购买</text>
                            </view>
                            <view class="round buy text-sm shadow bg-red" :class="'bg-' + theme.themeColor" v-else
                                @click.stop="exconfirm(item)">
                                <text> 立即兑换</text>
                            </view>
                        </view>
                    </view>
                </view>
            </view>
        </view>
    </view>
</template>

<script>
    const app = getApp();
    import api from "utils/api";
    export default {
        data() {
            return {
                CustomBar: this.CustomBar,
                theme: app.globalData.theme, //全局颜色变量
                modalCoupon: false,
                mask: false,
                goodsId: "",
            };
        },
        props: {
            goodsList: {
                type: Array,
                default: () => [],
            },
        },
        methods: {
            click1(id, shopId) {
                uni.navigateTo({
                    url: "/pages/goods/goods-detail/index?id=" + id + "&shopId=" + shopId,
                });
            },
            showShops(id) {
                uni.$emit("goodsId", id);
                this.goodsId = id;
                this.modalCoupon = true;
                this.mask = true;
            },
            handel() {
                this.mask = false;
            },
            //兑换商品
            async payHandel(index, goodsPoints, quantity) {
                // const index = e.currentTarget.dataset.index;
                this.$emit('onSelectNum', index)
                // try {
                //   // let { data: goodsSpu } = await api.goodsGet(id);
                //   // console.log(goodsSpu);
                //   let goodsSpu = this.goodsList[index].goodsSpu;
                //   let param = {
                //     spuId: goodsSpu.id,
                //     goodsPoints: goodsPoints,
                //     shopInfo: goodsSpu.shopInfo,
                //     quantity: 1,
                //     marketId: this.goodsList[index].id, // 1营销id
                //     itemType: '5', // 积分商品
                //     salesPrice: goodsSpu.dsPrice,
                //     spuName: goodsSpu.name,
                //     picUrl: goodsSpu.picUrls[0] || '',
                //     pointsDeductSwitch: goodsSpu.pointsDeductSwitch,
                //     pointsDeductScale: goodsSpu.pointsDeductScale,
                //     pointsDeductAmount: goodsSpu.pointsDeductAmount,
                //     pointsGiveSwitch: goodsSpu.pointsGiveSwitch,
                //     pointsGiveNum: goodsSpu.pointsGiveNum,
                //     freightTemplat: goodsSpu.freightTemplat,
                //     factory: goodsSpu.factory,
                //     specs: goodsSpu.specs,
                //     weight: goodsSpu.weight,
                //     volume: goodsSpu.volume,
                //   };
                //   uni.setStorageSync("param-orderConfirm", [param]);
                //   uni.navigateTo({
                //     url: "/pages/order/order-confirm/points?type=4",
                //   });
                //   // router.push({ path: "/orderConfirm", query: { type: 4 } });
                // } catch (error) {
                //   console.error(error.message || error.msg);
                // }
            },
            // 确认提示
            async exconfirm(item) {
                uni.showModal({
                    title: "提示",
                    content: "确认兑换吗？",
                    success: (res) => {
                        if (res.confirm) {
                            this.exchange(item);
                        }
                    },
                });
            },
            // 兑换优惠券
            async exchange(item) {
                try {
                    // 确认提示
                    let params = {
                        id: item.id,
                        type: item.type,
                        objectId: item.objectId,
                        qty: '1',
                    }
                    await api.subPointOrder(params);
                    uni.showModal({
                        title: "成功提示",
                        content: "兑换成功",
                        success: (res) => {
                            this.$emit("pointReload");
                        },
                    });
                } catch (error) {
                   
                    console.log(error,'errrrrrrrrrrr');
                }
            },
        },
    };
</script>
<style>
    .goods-item {
        width: 94% !important;
        margin: auto !important;
        margin-top: 20upx !important;
        padding-left: 10upx;
    }

    .row-img {
        margin-top: 26upx;
        margin-left: -14upx;
        width: 240upx !important;
        height: 240upx !important;
        border-radius: 10upx;
    }

    .buy {
        padding: 10upx 20upx 10upx 20upx;
    }

    /* 	.content{
		width: 100%;
		height: 200upx;
	} */
    .goods-item {
        margin-top: 32rpx !important;
    }

    .cu-card.article>.cu-item {
        padding-bottom: 15upx;
    }

    .cu-card.article>.cu-item .content {
        position: relative;
        padding: 0 10upx;
    }


    .cfBox {
        width: 100upx;
        height: 40upx;
        line-height: 40upx;
        font-size: 22upx;
        text-align: center;
        background-color: #00a6ba;
        position: absolute;
        left: 0;
        top: 0;
        color: white;
        border-radius: 10em;
    }
</style>